using SqlSugar;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Util.Config;

namespace Util.DataBase
{
    public class DbContextFactory
    {
        /// <summary>
        /// 创建数据访问上下文
        /// </summary>
        /// <param name="bol_share_thread">实例是否共享线程</param>
        /// <returns></returns>
        public static SqlSugarClient CreateDbContext(bool bol_share_thread)
        {
            string dbType = AppSettingConfig.GetConnectionString("DbType");
            Assembly assembly = Assembly.Load($"Util.Database");
            IContext dbContext = assembly.CreateInstance($"Util.Database.{dbType}Context", true) as IContext;
            return dbContext.GetDbContext(bol_share_thread, null);
        }

        /// <summary>
        /// 以指定的字串所属的键值创建数据访问上下文
        /// </summary>
        /// <param name="connKey">键值</param>
        /// <param name="bol_share_thread">实例是否共享线程</param>
        /// <returns></returns>
        public static SqlSugarClient CreateDbContext(string connKey, bool bol_share_thread)
        {
            string dbType = AppSettingConfig.GetConnectionString("DbType");
            Assembly assembly = Assembly.Load($"Util.Database");
            IContext dbContext = assembly.CreateInstance($"Util.Database.{dbType}Context", true) as IContext;
            return dbContext.GetDbContext(bol_share_thread, connKey);
        }
    }
}
